package calendar.demo;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class CalendarActivity extends Activity {

	private LinearLayout mainLayout = null;
	private int availableWidth;
	private int availableHeight;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        
        /**
         * 屏幕高宽
         * Canvas对象 、display对象和DisplayMetrics可获取屏幕的高宽
         * 
         * 状态栏高度 
         * View的getWindowVisibleDisplayFrame(Rect outRect)附值outRect后，outRect.top()即是状态栏高度
         * 
         * 标题高度
         * View的getWindowVisibleDisplayFrame(Rect outRect1)附值outRect后，outRect.height()-view.getheight()即是标题高度。
         * 
         */
        
        //获取系统当前显示的view根（是一个framelayout对象），android绘制会将要绘制的view放置在framelayout中绘制。 
        //View view = this.getWindow().findViewById(Window.ID_ANDROID_CONTENT);
        //Display display = this.getWindowManager().getDefaultDisplay();
        
        DisplayMetrics displayMetrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        
        //Rect rect = new Rect();
        //this.getDrawingRect(rect);
        //this.getWindowVisibleDisplayFrame(rect);
        
        availableWidth = displayMetrics.widthPixels;
        availableHeight = displayMetrics.heightPixels;
        
        ScrollView scrollView = new ScrollView(this);
        scrollView.addView(createCalendar());
        scrollView.setBackgroundResource(R.drawable.bg);
        setContentView(scrollView);
    }
    
    public View createCalendar() {
    	LinearLayout rowLayout = null;
    	LinearLayout timeTagLayout =  null;
    	LinearLayout timeCellLayout  =  null;
    	TimeCell timeCell = null;
    	TimeTag timeTag = null;
    	
    	mainLayout= createLayout(LinearLayout.VERTICAL);

    	for (int i = 0; i < 24; i++) {
    		rowLayout = createLayout(LinearLayout.HORIZONTAL);
    		timeTagLayout = createLayout(LinearLayout.VERTICAL);
    		timeCellLayout  = createLayout(LinearLayout.VERTICAL);
    		
    		timeTag = new TimeTag(this, 50, availableHeight / 8, String.valueOf(i));
    		timeTagLayout.addView(timeTag);
    		
    		timeCell = new TimeCell(this, availableWidth - 50, availableHeight / 16);
    		timeCell.setOnTimeCellClick(timeCellClick);
    		timeCellLayout.addView(timeCell);
    		
    		timeCell = new TimeCell(this, availableWidth - 50, availableHeight / 16);
    		timeCell.setOnTimeCellClick(timeCellClick);
    		timeCellLayout.addView(timeCell);
    		
    		rowLayout.addView(timeTagLayout);
    		rowLayout.addView(timeCellLayout);

    		mainLayout.addView(rowLayout);
    	}
    	
    	return mainLayout;
    }
    
    public LinearLayout createLayout(int orientation) {
    	LinearLayout layout = new LinearLayout(this);
    	//LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    	
    	//设置该view相对与父view 的位置．比如一个button 在linearlayout里，你想把该button放在linearlayout里靠左、靠右等位置就可以通过该属性设置． 
        //lp.gravity = gravity; 
    	
    	/*
    	LinearLayout layout = new LinearLayout(this);
    	layout.setLayoutParams(new LayoutParams(  
                 android.view.ViewGroup.LayoutParams.FILL_PARENT,  
                 android.view.ViewGroup.LayoutParams.WRAP_CONTENT)); 
    	*/
    	// 设置布局方向
    	layout.setOrientation(orientation);
    	
    	return layout;
    }
    
    private TimeCell.OnTimeCellClick timeCellClick = new TimeCell.OnTimeCellClick() {
		
		public void OnClick(TimeCell timeCell) {
			//timeCell.setSelected(true);  
			mainLayout.invalidate();
		}
	};

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		return super.onCreateDialog(id);
	}
}